package cn.t.dynamic.switcher.demo;

import cn.t.dynamic.switcher.router.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"cn.t.dynamic.switcher.demo.mapper"})
public class DemoConfig {

    private final static String[] MAPPER_XML_PATH = {"classpath*:cn/t/dynamic/**/*.xml"};


    @Bean("prop1")
    @ConfigurationProperties(prefix = "spring.datasource.datasource1")
    public DataSourceProperties firstDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * 数据源1根据注入的数据配置信息1进行构造
     *
     * @return
     */
    @Bean(name = "datasource1")
    public DataSource datasource1() {
        // 该方法不再适用!DataSource中并没有对应的属性设置
        // var ds = DataSourceBuilder.create().build();
        return firstDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 配置数据源1的事务管理器
     * @param dynamicDataSource 动态数据源
     * @return
     */
    @Bean(name = "tx1")
    @Primary
    public PlatformTransactionManager txManager1(DynamicDataSource dynamicDataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dynamicDataSource);
        // 事务10秒超时
        transactionManager.setDefaultTimeout(10);
        return transactionManager;
    }

    /**
     * 配置sqlsession工厂，mybatis的操作都由sqlsession来完成的
     * @param dynamicDataSource
     * @return
     * @throws Exception
     */
    @Primary
    @Bean("sf1")
    public SqlSessionFactory ds1SqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dynamicDataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources(MAPPER_XML_PATH[0]));

        // mybatis-plus配置 配置增强
        var configuration = new org.apache.ibatis.session.Configuration();
        // 插入或者更新数据时,如果对象字段为null，则插入/更新为null，不设此属性需要在xml中指定JdbcType
        // 开启驼峰映射
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactory.setConfiguration(configuration);
        // 配置了以后xml文件中的resultType和parameterType就不需要指定全类名xx.xx.class，我们只需要写class，会到我们配置的typeAliasesPackage包下搜索
        // sqlSessionFactory.setTypeAliasesPackage(DATASOURCE1);
        return sqlSessionFactory.getObject();
    }

    /**
     * 配置sqlsession
     * @param sqlSessionFactory
     * @return
     */
    @Primary
    @Bean(name = "sp1")
    public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("sf1") SqlSessionFactory sqlSessionFactory) {
        // 最终操作mapper的是这个template
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
